﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Core.Metadata;
using Luna.WPF.ApplicationFramework;
using XWord.Fundamental.Domain.Model;
using Microsoft.Practices.ServiceLocation;
using XWord.Fundamental.Domain;
using System.Threading;
using XWord.WPF.ApplicationFramework;
using XWord.GUI.Presenters.Interface;
using XWord.Fundamental.Domain.Services;
using System.Windows.Threading;
using XWord.WPF.ApplicationFramework.Threads;
using XWord.Common;
using System.Collections.Specialized;
using System.Windows;
using XWord.Common.Extensions;
namespace XWord.GUI.Presenters.Fundamental
{
    [Singleton(typeof(WordTabPresenter))]
    public partial class WordTabPresenter : DockingPresenter, ITabPresenter
    {
        private IServiceLocator _serviceLocator;
        public WordTabPresenter(IServiceLocator entityFactory)
        {
            this.Index = 0;
            _serviceLocator = entityFactory;
            ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) => {
            AlphabetList =BinaryFormatterHelper.Deserialize<IList<Alphabet>>(BinaryFiles.AlphabetList).ToList();
            }));
            Application.Current.Exit += new ExitEventHandler(Current_Exit);
            _delayTimer.InitInvoke(0.15, DefaultSearch);
        }

        void Current_Exit(object sender, ExitEventArgs e)
        {
            BinaryFormatterHelper.Serialize(BinaryFiles.AlphabetList, AlphabetList.ToArray());
        }

        protected override void OnInitialize()
        {
            XWordAppConfig.Current.LatestSearchList.CollectionChanged += new NotifyCollectionChangedEventHandler(LatestSearchList_CollectionChanged);
            base.OnInitialize();
        }

        void LatestSearchList_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    WordBehavior.RaiseWordEventHandler(new Word() { Name = e.NewItems[0].ToString() }, WordAction.SelectedItemChanged);
                    break;
            }
        }

        private IList<Alphabet> _alphabetList;
        public IList<Alphabet> AlphabetList
        {
            get { return _alphabetList ?? (_alphabetList = new List<Alphabet>()); }
            set
            {
                _alphabetList = value;
                NotifyOfPropertyChange("AlphabetList");
            }
        }

        public int SearchMode
        {
            get { return XWordAppConfig.Current.SearchTarget; }
            set
            {
                XWordAppConfig.Current.SearchTarget = value;
                NotifyOfPropertyChange("SearchMode");
                SearchEtyma(SearchText);
            }
        }

        private string _searchText;
        public string SearchText
        {
            get { return _searchText; }
            set
            {
                _searchText = value;
                NotifyOfPropertyChange("SearchText");
            }
        }

        private DelayTimer _delayTimer = new DelayTimer();

        private Alphabet _selectedAlphabet;
        public Alphabet SelectedAlphabet
        {
            get { return _selectedAlphabet; }
            set
            {
                _selectedAlphabet = value;
                if (value != null)
                {
                    this.SearchText=value.Name;
                }
                NotifyOfPropertyChange("SelectedAlphabet");
            }
        }

        public int Index { get; set; }

        #region Presenters

        private IWordExplorerPresenter _wordExplorerPresenter;
        private IWordExplorerPresenter WordExplorerPresenter
        {
            get
            {
                if (_wordExplorerPresenter == null)
                {
                    _wordExplorerPresenter = _serviceLocator.GetInstance<IWordExplorerPresenter>();
                }
                return _wordExplorerPresenter;
            }
        }

        private IEtymaLibraryExplorerPresenter _etymaLibraryExplorerPresenter;
        private IEtymaLibraryExplorerPresenter EtymaLibraryExplorerPresenter
        {
            get
            {
                if (_etymaLibraryExplorerPresenter == null)
                {
                    _etymaLibraryExplorerPresenter = _serviceLocator.GetInstance<IEtymaLibraryExplorerPresenter>();
                }
                return _etymaLibraryExplorerPresenter;
            }
        }

        public IWordBehavior WordBehavior { get; set; }
        public IDockSitePresenter DockSitePresenter { get; set; }

        #endregion
    }
}
